//
//  TSMsgContentItemCell.m
//  chart
//
//  Created by xu on 15/7/31.
//  Copyright (c) 2015年 xu. All rights reserved.
//

#import "TSMsgContentItemCell.h"


#define TITLE_TEXT_FIELD_WIDTH 80.f

@implementation TSMsgContentItemCell

@synthesize titleLabel, contentTextField, notShowKeyboard;

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self=[super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self addTitleLabel];
        
        [self addTextField];
        
        [NOTIFICATIONCENTER addObserver:self selector:@selector(listenResignFirstResponder:) name:TSNotification_ResignFirstResponder object:nil];
    }
    return self;
}

- (void)setTitleAndTextField:(NSString *)title Type:(Text_Field_InputView_Type)type
{
    if (titleLabel) {
        [titleLabel setText:title];
    }
}

- (void)addTitleLabel
{
    titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(MARGIN_LEFT, MARGIN_TOP, TITLE_TEXT_FIELD_WIDTH, CELL_HEIGHT-2*MARGIN_TOP)];
    [titleLabel setFont:FONT(12.f)];
    [self addSubview:titleLabel];
}

- (void)addTextField
{
    contentTextField = [[TSToolBarTextField alloc] initWithFrame:CGRectMake(MARGIN_LEFT*2+TITLE_TEXT_FIELD_WIDTH, MARGIN_TOP, currentDeviceWidth-TITLE_TEXT_FIELD_WIDTH-MARGIN_LEFT*3, CELL_HEIGHT-2*MARGIN_TOP)];
    contentTextField.layer.borderWidth = 1.f;
    contentTextField.layer.borderColor = [UIColor lightGrayColor].CGColor;
    
    [self addSubview:contentTextField];
}

- (void)removeAllSubviews
{
    for (UIView *view in self.subviews) {
        [view removeFromSuperview];
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
}

- (void)listenResignFirstResponder:(NSNotification *)noti
{
    if (contentTextField && [contentTextField isFirstResponder]) {
        [contentTextField resignFirstResponder];
    }
}

- (void)setNotShowKeyboard:(BOOL)noKeyboard
{
    if (contentTextField) {
        contentTextField.notShowKeyboard = noKeyboard;
    }
}

- (void)setTextFieldEditable:(BOOL)isEditable
{
    if (contentTextField) {
        contentTextField.editable  = isEditable;
        if (!isEditable) {
            contentTextField.layer.borderColor = [UIColor clearColor].CGColor;
        }
    }
}

- (void)setToolBarTextFieldType:(NSInteger)toolBarTextFieldType
{
    if (contentTextField) {
        [contentTextField setToolBarTextFieldType:toolBarTextFieldType];
    }
}

- (void)dealloc
{
    [NOTIFICATIONCENTER removeObserver:self];
}
-(UIButton *)replaceRightWithButton:(CGSize)size
{
    [contentTextField removeFromSuperview];
    
    UIButton * newView = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    newView.frame =CGRectMake(MARGIN_LEFT*2+TITLE_TEXT_FIELD_WIDTH,-10, size.width,size.height);
    [self addSubview:newView];
    return newView;
}

-(void)setTextFieldSection:(NSInteger)section
{
    if(contentTextField){
        contentTextField.section = section;
    }
}

@end
